<?php
	require_once('../includes/config.php');
	$action = "add";
	$statusbtn = "Add new";
	$readonly = "";
	$newstitle = "";
	$newsdetail  = "";
	$newsimg = "";
	if($_REQUEST['act'] == "edit")
	{
		$newsid = $_GET['id'];
		$statusbtn = "Update";
		$readonly = "readonly";
		$str_query = "SELECT * FROM tbl_news WHERE id = '$newsid'";
		$resul = mysql_query("$str_query") or die ("can not load data for update ");
		$data = mysql_fetch_assoc($resul);
		$newstitle = $data['news_title'];
		$newsdetail = $data['news_detail'];
		$newsimg = $data['news_img'];
		$action = "edit";
		
	}
?>
<script type="text/javascript">
$(document).ready(function(){
	var title 		= $('#news_title');
	var titleInfo 	= $('#titleInfo');
	var newsimg 	= $('#news_img');
	var imgInfo 	= $('#imgInfo');
	var detail 		= $('#news_detail');
	var detailInfo 	= $('#detailInfo');
	title.blur(valTitle);
	newsimg.blur(valImg);
	title.keyup(valTitle);
	newsimg.keyup(valImg);
	detail.keyup(valDetail);
	function valTitle(){
		if(title.val().length < 3){
			titleInfo.addClass('error');
			titleInfo.text('News title must more than 3 char');
			titleInfo.removeClass('isok');
			return false;
		}
		else {
			titleInfo.removeClass('error');
			titleInfo.addClass('isok');
			titleInfo.text('Look like it is nice title');
			return true;
		}
	}
	function valImg(){
		var imageRegex = /(.*?)\.(jpg|jpeg|png|gif)$/;
		var x = newsimg.val();
		if(x.length > 3 && imageRegex.test(x)){
			imgInfo.removeClass('error');
			imgInfo.addClass('isok');
			imgInfo.text('You can use this image!');
			return true;	
		}
		else {
			imgInfo.addClass('error');
			imgInfo.removeClass('isok');
			imgInfo.text('This is not a vaild image!');
			return false;
		}
	}
	function valDetail(){
		if(encodeURIComponent(detail.attr('value').length) < 10) {
			detailInfo.addClass('error');
			detailInfo.removeClass('isok');
			return false;
		}
		else {
			detailInfo.addClass('isok');
			detailInfo.removeClass('error');
			return true;
		}
	}
	$("form#newsForm").submit(function(){
		if(valTitle() & valImg() & valDetail()){
			var id = $('#news_id').attr('value');
			var act = $("#act").attr('value');
			var datastring = 'act=' + act +
							 '&id=' + id +
							 '&title=' + title.attr('value') +
							 '&detail=' + encodeURIComponent(detail.attr('value')) +
							 '&newsimg=' + newsimg.attr('value');
			 $.ajax({ 
				 url: "news_process.php",
				 type: "POST",
				 data: datastring,
				 dataType: 'json',
				 cache: false,
				 success: function(data){
					 if(data==1)
					 {
						$("#rounded-corner").load("news_list.php");
						disablePopup();
						$("form").each( function() { this.reset; });
					 }
					 else
					 {
						 alert(data);
					 }
				 }
			 });
			 return false;
		}
		else
			return false;
	});
});
</script>
 <link rel="stylesheet" href="templates/redactor/css/redactor.css" />
<script src="templates/redactor/redactor.js"></script>
<script type="text/javascript"> 
	$(document).ready(
		function()
		{
			$('#news_detail').redactor({ focus: true });
			$('#news_detail').redactor({ autoresize: false });
		}
	);
</script>
		<a id="popupNewsClose">x</a>
		<h1><?php echo $action; ?> News</h1>
        <form id="newsForm" method="post">
            <input type="hidden" name="news_id" id="news_id" value="<?php echo $newsid; ?>" />
            <p>
                <label>News Title:</label>
                <input type="text" name="news_title" id="news_title" value="<?php echo $newstitle; ?>" />
                <span id="titleInfo"</span>
            </p>
            <p>
                <label>News image:</label>
                <input type="text" name="news_img" id="news_img" value="<?php echo $newsimg; ?>" />
                <span id="imgInfo"></span>
            </p>
            <fieldset>
            <legend id="detailInfo">Detail</legend>
            <p>
                <textarea id="news_detail" name="news_detail" cols="20" rows="5" style="height: 320px"><?php echo $newsdetail; ?></textarea>
            </p>
            </fieldset>
                <button id="act" type="submit" name="act" value="<?php echo $action; ?>"><span class="buttons"><?php echo $action; ?></span></button>
        </form>
		
	
